package twitter 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.setTimeout;
	import twitter.Twitter;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class TwitterItem extends Sprite implements ITwitterItem
	{
		private var twitterCaller:Twitter;
		private var square:Shape = new Shape();
		
		public function TwitterItem() 
		{
			
			if( StaticProperties.debug ) trace( 'new TwitterItem' );
			
			square.x = StaticProperties.stageWidth * .5;
			square.y = StaticProperties.stageHeight * .5;
			
			square.graphics.beginFill( 0x808080 + Math.random() * 0x808080 );
			square.graphics.drawRect( -50, -50, 100, 100 );
			square.alpha = 0;
			square.scaleX = square.scaleY = 15;
			
			addChild( square );
		}
		
		
		/////////////////////////////////////////////////////////////////////
		//
		//
		//					OUVRIR
		//
		//
		/////////////////////////////////////////////////////////////////////
		
		
		// ouvre l'animation
		public function open():void
		{
			
			if( StaticProperties.debug ) trace( this, 'open' );
			addEventListener( Event.ENTER_FRAME, openingLoop );
			
		}
		
		//animation d'ouverture
		private function openingLoop(e:Event):void 
		{
			square.alpha += .05;
			if ( square.alpha >= 1 ) onOpened();
		}
		
		//puis dispatche un event TwitterEvent.OPENED
		private function onOpened():void 
		{
			if( StaticProperties.debug ) trace( this, 'opened' );
			removeEventListener( Event.ENTER_FRAME, openingLoop );
			dispatchEvent( new TwitterEvent( TwitterEvent.OPENED ) );
		}
		
		/////////////////////////////////////////////////////////////////////
		//
		//
		//					YOUPI !
		//
		//
		/////////////////////////////////////////////////////////////////////
		
		//lance l'animation
		public function start():void 
		{
			
			if ( StaticProperties.debug ) trace( this, 'start' );
			if ( StaticProperties.debug ) trace( this, 'calling twitter' );
			
			twitterCaller = new Twitter();
			twitterCaller.addEventListener( Event.COMPLETE, twitterCalReady );
			twitterCaller.request( 'youpi', '5' );
			
		}
		
		private function twitterCalReady(e:Event):void 
		{
			if ( StaticProperties.debug ) trace( this, 'twiter call complete' );
			twitterCaller.removeEventListener( Event.COMPLETE, twitterCalReady );
			var offset:int = 10;
			for each( var entry:TwitterEntry in twitterCaller.entries )
			{
				if ( StaticProperties.debug ) trace( entry.authorName, ':', entry.content );
				var txt:TextField = new TextField();
				txt.autoSize = TextFieldAutoSize.LEFT;
				txt.htmlText = entry.authorName + ':' + entry.content;
				txt.setTextFormat( new TextFormat( 'Verdana', 10, 0xFFFFFF ) )
				addChild( txt );
				txt.y = offset;
				offset += 16;
			}
			
			addEventListener( Event.ENTER_FRAME, update );
			
		}
		
		//fait des choses incroyables
		private function update(e:Event):void 
		{
			
			square.rotation += 1;
			square.alpha = Math.cos( square.rotation / 180 * Math.PI ) * .5 + .5;
			square.scaleX += ( 1 - square.scaleX ) * .05;
			square.scaleY += ( 1 - square.scaleY ) * .05;
			
		}
		
		/////////////////////////////////////////////////////////////////////
		//
		//
		//					FERMER
		//
		//
		/////////////////////////////////////////////////////////////////////
		
		
		// ferme l'animation
		public function close():void
		{
			if ( StaticProperties.debug ) trace( this, 'close' );
			
			removeEventListener( Event.ENTER_FRAME, update );
			addEventListener( Event.ENTER_FRAME, closingLoop );
			
		}
		
		//animation de fermeture
		private function closingLoop(e:Event):void 
		{
			
			square.rotation += 1;
			square.alpha = Math.cos( square.rotation / 180 * Math.PI ) * .5 + .5;
			square.scaleX -= .1;
			square.scaleY = square.scaleX;
			if ( square.scaleX <= 0 ) onClosed();
			
		}
		
		//puis dispatche un event TwitterEvent.CLOSED
		private function onClosed():void 
		{
			if( StaticProperties.debug ) trace( this, 'closed' );
			removeEventListener( Event.ENTER_FRAME, closingLoop );
			dispatchEvent( new TwitterEvent( TwitterEvent.CLOSED ) );
		}
		
	}

}